﻿package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements SensorEventListener {

    private TextView tv_value1;
    private TextView tv_value2;
    private TextView tv_value3;
    private SensorManager sManager;
    private Sensor mSensorOrientation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sManager=(SensorManager) getSystemService(SENSOR_SERVICE);
        mSensorOrientation=sManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        sManager.registerListener(this,mSensorOrientation, SensorManager.SENSOR_DELAY_UI);
        bindViews();

    }

    private void bindViews() {
        tv_value1=(TextView)findViewById(R.id.tv_value1);
        tv_value2=(TextView)findViewById(R.id.tv_value2);
        tv_value3=(TextView)findViewById(R.id.tv_value3);
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        tv_value1.setText("方位角:"+(float)(Math.round(sensorEvent.values[0]*100))/100);
        tv_value2.setText("倾斜角:"+(float)(Math.round(sensorEvent.values[1]*100))/100);
        tv_value3.setText("滚动角:"+(float)(Math.round(sensorEvent.values[2]*100))/100);


    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }
}
